package cn.iocoder.yudao.module.research.controller.admin.syslog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.research.controller.admin.syslog.vo.*;
import cn.iocoder.yudao.module.research.dal.dataobject.syslog.SyslogDO;
import cn.iocoder.yudao.module.research.service.syslog.SyslogService;

@Tag(name = "管理后台 - syslog")
@RestController
@RequestMapping("/research/syslog")
@Validated
public class SyslogController {

    @Resource
    private SyslogService syslogService;

    @PostMapping("/create")
    @Operation(summary = "创建syslog")
    @PreAuthorize("@ss.hasPermission('research:syslog:create')")
    public CommonResult<Long> createSyslog(@Valid @RequestBody SyslogSaveReqVO createReqVO) {
        return success(syslogService.createSyslog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新syslog")
    @PreAuthorize("@ss.hasPermission('research:syslog:update')")
    public CommonResult<Boolean> updateSyslog(@Valid @RequestBody SyslogSaveReqVO updateReqVO) {
        syslogService.updateSyslog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除syslog")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('research:syslog:delete')")
    public CommonResult<Boolean> deleteSyslog(@RequestParam("id") Long id) {
        syslogService.deleteSyslog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得syslog")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('research:syslog:query')")
    public CommonResult<SyslogRespVO> getSyslog(@RequestParam("id") Long id) {
        SyslogDO syslog = syslogService.getSyslog(id);
        return success(BeanUtils.toBean(syslog, SyslogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得syslog分页")
    @PreAuthorize("@ss.hasPermission('research:syslog:query')")
    public CommonResult<PageResult<SyslogRespVO>> getSyslogPage(@Valid SyslogPageReqVO pageReqVO) {
        PageResult<SyslogDO> pageResult = syslogService.getSyslogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SyslogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出syslog Excel")
    @PreAuthorize("@ss.hasPermission('research:syslog:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSyslogExcel(@Valid SyslogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SyslogDO> list = syslogService.getSyslogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "syslog.xls", "数据", SyslogRespVO.class,
                        BeanUtils.toBean(list, SyslogRespVO.class));
    }

    @PostMapping("/analysis")
    @Operation(summary = "解析syslog")
    @PreAuthorize("@ss.hasPermission('research:syslog:create')")
    public CommonResult<String> analysisSyslog(@Valid @RequestBody SyslogSaveReqVO createReqVO) {
        return success(syslogService.analysisSyslog(createReqVO.getId()));
    }

}